using System;
using System.IO;
using System.Text;
/// <summary>
/// Summary description for LogFile
/// </summary>
public class LogFile
{
    /// <summary>
    /// Ghi thong tin ra file Log + dd/mm/yyyy 
    /// </summary>
    /// <param name="LogText">Noi dung can ghi ra file Log</param>
    /// <returns>
    ///     True: Neu ghi thanh cong
    ///     False: Neu ghi ko thanh cong
    /// </returns>
    public static bool SaveInfoLog(string LogText)
    {
        try
        {
            string sPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
            string fileName = sPath + "\\Log\\Info" + System.DateTime.Now.ToString("ddMMyyyy") + ".dat";
            TextWriter writer = new StreamWriter(fileName, true, Encoding.Unicode);
            try
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "---" + LogText);
                return true;
            }
            catch (System.IO.DirectoryNotFoundException ex)
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "--- Khong tim thay file, thu muc! " + ex.Message);
                return false;
            }
            catch (Exception ex)
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "---" + LogText + ex.Message);
                return false;
            }
            finally
            {
                writer.Close();
            }
        }
        catch (System.UnauthorizedAccessException ex)
        {
            throw new UnauthorizedAccessException("Khong co quyen truy cap thu muc, file ! " + ex.ToString());
        }
        catch (System.NotSupportedException ex)
        {
            throw new NotSupportedException(ex.Message);
        }
    }
    public static bool SaveReceiverMOLog(string LogText)
    {
        try
        {
            string sPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
            string fileName = sPath + "\\Log\\ReceiverMO\\MO" + System.DateTime.Now.ToString("ddMMyyyy") + ".dat";
            TextWriter writer = new StreamWriter(fileName, true, Encoding.Unicode);
            try
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "---" + LogText);
                return true;
            }
            catch (System.IO.DirectoryNotFoundException ex)
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "--- Khong tim thay file, thu muc! " + ex.Message);
                return false;
            }
            catch (Exception ex)
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "---" + LogText + ex.Message);
                return false;
            }
            finally
            {
                writer.Close();
            }
        }
        catch (System.UnauthorizedAccessException ex)
        {
            throw new UnauthorizedAccessException("Khong co quyen truy cap thu muc, file ! " + ex.ToString());
        }
        catch (System.NotSupportedException ex)
        {
            throw new NotSupportedException(ex.Message);
        }
    }
    public static bool SaveSendMTLog(string LogText)
    {
        try
        {
            string sPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
            string fileName = sPath + "\\Log\\SendMT\\MT" + System.DateTime.Now.ToString("ddMMyyyy") + ".dat";
            TextWriter writer = new StreamWriter(fileName, true, Encoding.Unicode);
            try
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "---" + LogText);
                return true;
            }
            catch (System.IO.DirectoryNotFoundException ex)
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "--- Khong tim thay file, thu muc! " + ex.Message);
                return false;
            }
            catch (Exception ex)
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "---" + LogText + ex.Message);
                return false;
            }
            finally
            {
                writer.Close();
            }
        }
        catch (System.UnauthorizedAccessException ex)
        {
            throw new UnauthorizedAccessException("Khong co quyen truy cap thu muc, file ! " + ex.ToString());
        }
        catch (System.NotSupportedException ex)
        {
            throw new NotSupportedException(ex.Message);
        }
    }
    /// <summary>
    /// Ghi thong tin cua doi tuong Error ra file Log
    /// </summary>
    /// <param name="err">Error Object</param>
    /// <returns>
    ///     True: Neu ghi thanh cong
    ///     False: Neu ghi ko thanh cong
    /// </returns>
    public static bool SaveErrLog(Exception err)
    {
        try
        {
            string sPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
            string fileName = sPath + "\\Log\\Error\\Error" + System.DateTime.Now.ToString("ddMMyyyy") + ".dat";
            TextWriter writer = new StreamWriter(fileName, true, Encoding.Unicode);
            try
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "---" + err.ToString());
                return true;
            }
            catch (System.IO.DirectoryNotFoundException ex)
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "--- Khong tim thay file, thu muc! " + ex.Message);
                return false;
            }
            catch (Exception ex)
            {
                writer.WriteLine(System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + "---" + ex.Message + err.ToString());
                return false;
            }
            finally
            {
                writer.Close();
            }
        }
        catch (System.UnauthorizedAccessException ex)
        {
            throw new UnauthorizedAccessException("Khong co quyen truy cap thu muc, file ! " + ex.ToString());
        }
        catch (System.NotSupportedException ex)
        {
            throw new NotSupportedException(ex.Message);
        }
    }
}
